package com.nuanshui.heatedloan.msgcenter.apppush.spring;

import com.nuanshui.heatedloan.msgcenter.apppush.AppPushFacade;
import com.nuanshui.heatedloan.msgcenter.apppush.annotation.AppPushFilterProvider;
import com.nuanshui.heatedloan.msgcenter.apppush.filter.AppPushFilter;
import com.nuanshui.heatedloan.msgcenter.apppush.filter.AppPushFilterWrapper;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

/**
 * 推送注解处理器
 * @param 
 * @author guohao
 * @date 2017年2月10日 下午4:55:11
 * @since $
 */
@Component
public class AppPushProcess implements BeanPostProcessor,FactoryBean<AppPushFacade>{

	
	private AppPushFacade appPushFacade = new AppPushFacade();
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		
		Class<?> targetClass = AopUtils.getTargetClass(bean);
			
	    AppPushFilterProvider appPushFilterProvide =  AnnotationUtils.getAnnotation(targetClass, AppPushFilterProvider.class);
		
		if(appPushFilterProvide!=null &&  bean instanceof  AppPushFilter) {
			AppPushFilterWrapper wrapper = new AppPushFilterWrapper();
			wrapper.setTarget((AppPushFilter)bean);
			wrapper.setOrder(appPushFilterProvide.order());
			appPushFacade.getWrapper().register(wrapper);//过滤器注册
		}
		
		return bean;
	}

	@Override
	public AppPushFacade getObject() throws Exception {	
		return appPushFacade;
	}

	@Override
	public Class<?> getObjectType() {
		return AppPushFacade.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	
	
}
